package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import sql.Conexion;
import sql.IConexion;

public class DAO {

	private IConexion iConexion;
	
	public DAO(){
		this.iConexion = Conexion.obtenerInstancia();
	}
	
	public IConexion getiConexion() {
		return iConexion;
	}
	
	public ResultSet getObjectById(String tableName, String idColumnName, int idValue){
		String query = "select * from " + tableName + " where " + idColumnName + " = " + idValue;
		try {
			return this.getiConexion().ejecutarPreparedStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	}
	
	public ResultSet getObjectByName(String tableName, String ColumnName, String Value){
		String query = "select * from " + tableName + " where " + ColumnName + " = " + "'" + Value + "'";
		try {
			return this.getiConexion().ejecutarPreparedStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	}
	
	public ResultSet getObjectAll(String tableName){
		String query = "select * from " + tableName;
		try {
			return this.getiConexion().ejecutarPreparedStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	}

	public ResultSet executeQuery(String query) {
		try {
			return this.getiConexion().ejecutarPreparedStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	
	
	}

	public void persist(String query) {
		try {
			this.getiConexion().ejecutarSatement(query);
		} catch (SQLException e) {
			e.printStackTrace();
		};
	}

	public ResultSet getObjectByIdWhereX2(String tableName, String idColumnName,
			String idValue, String idColumnName2, int codigoEmpresa) {
		// TODO Auto-generated method stub
		String query = "select * from " + tableName + " where " + idColumnName + " = '" + idValue + "' and " + idColumnName2 + " = " + codigoEmpresa;
		try {
			return this.getiConexion().ejecutarPreparedStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		return null;
	}

	public PreparedStatement prepareStatement(String query) {
		// TODO Auto-generated method stub
		PreparedStatement ps = null;
		try {
			ps = this.getiConexion().prepareStatement(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ps;
	}
	
	
	
}
